package cn.com.assistant.controller;

import cn.com.assistant.annotation.AdminLoginToken;
import cn.com.assistant.entity.AnswerEntity;
import cn.com.assistant.model.ImageDTO;
import cn.com.assistant.mapper.AnswerMapper;
import cn.com.assistant.mapper.CasAnswerMapper;
import cn.com.assistant.model.AnswerModel;
import cn.com.assistant.service.AnswerService;
import cn.com.assistant.service.CasAnswerService;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/answer")
public class AnswerController {
 
    @Autowired
    AnswerService answerService;
    @Autowired
    AnswerMapper answerMapper;
    @Autowired
    CasAnswerService casAnswerService;
    @Autowired
    CasAnswerMapper casAnswerMapper;

//    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/insert")
    @ApiOperation(value = "添加问题回复", notes = "添加问题回复")
    @ApiImplicitParam(name = "answerModel", value = "问题回复模型", required = true, dataType = "AnswerModel")
    public ResponseMessage insert(@RequestBody @Valid AnswerModel answerModel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        if (null != answerModel && CollectionUtils.isNotEmpty(answerModel.getAnswerEntities())) {
            List<AnswerEntity> answerEntities = answerModel.getAnswerEntities();
            Date date = new Date();
            for (int i = 0; i < answerEntities.size(); i++) {
                AnswerEntity answerEntity = answerEntities.get(i);
                answerEntity.setId(UUIDUtil.getUUID());
                answerEntity.setCreatedDate(date);
                // 处理多张图片，追加分号
                StringBuffer imgUrl = null;
                List<String> imgUrlList = answerEntity.getImgUrlList();
                if (CollectionUtils.isNotEmpty(imgUrlList)) {
                    for (int j = 0; j < imgUrlList.size(); j++) {
                        String url = imgUrlList.get(j) + ";";
                        imgUrl.append(url);
                    }
                }
                answerEntity.setImgUrl(String.valueOf(imgUrl));
            }
            boolean flag = answerService.saveBatch(answerEntities);
            if (flag) {
                return ResponseMessage.defaultResponse().setMsg("保存成功！");
            } else {
                return ResponseMessage.failResponse().setMsg("保存失败！");
            }
        }
        return ResponseMessage.failResponse().setMsg("保存失败！");
    }


    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/update")
    @ApiOperation(value = "修改一级问题回复", notes = "修改一级问题回复")
    @ApiImplicitParam(name = "answerEntity", value = "一级问题回复实体", required = true, dataType = "AnswerEntity")
    public ResponseMessage update(@RequestBody @Valid AnswerEntity answerEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        answerEntity.setUpdatedDate(new Date());
        boolean flag = answerService.updateById(answerEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/updateImage")
    @ApiOperation(value = "更新一级问题回复图片", notes = "更新一级问题回复图片")
    @ApiImplicitParam(name = "imageDTO", value = "更新图片模型", required = true, dataType = "ImageDTO")
    public ResponseMessage updateImage(@RequestBody @Valid ImageDTO imageDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        return answerService.updateImage(imageDTO);
    }


    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除一二级所有回复数据", notes = "删除一二级所有回复数据")
    @ApiImplicitParam(name = "回复数据id", value = "回复数据id")
    public ResponseMessage delete(@RequestParam("questionId") String questionId) {
        boolean flag = answerService.deleteByQuestionId(questionId);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("删除成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("删除失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @DeleteMapping("/deleteById")
    @ApiOperation(value = "删除一级问题回复", notes = "删除一级问题回复")
    @ApiImplicitParam(name = "一级问题回复id", value = "一级问题回复id")
    public ResponseMessage deleteById(@RequestParam("id") String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);

        if (CollectionUtils.isNotEmpty(ids)) {
            // 删除二级回复
            casAnswerMapper.deleteByPId(ids);
        }

        // 删除一级回复
        answerMapper.deleteById(id);
        return ResponseMessage.defaultResponse().setMsg("删除成功！");
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findByQId")
    @ApiOperation(value = "查询一级回复数据", notes = "查询一级回复数据")
    @ApiImplicitParam(name = "问题id", value = "问题id")
    ResponseMessage findByPid(@RequestParam("questionId") String questionId) {
        return answerService.findByQid(questionId);
    }

    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findByPage")
    @ApiOperation(value = "查询故障现象分页数据", notes = "查询故障现象分页数据")
    ResponseMessage findByPage(@RequestParam(value = "page", defaultValue = "0") Integer pageNum,
                               @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return answerService.findByPage(pageNum, size);
    }

    @GetMapping("/getAllFault")
    @ApiOperation(value = "获取所有故障及处理措施", notes = "获取所有故障及处理措施")
    ResponseMessage getAllFault() {
        return answerService.getAllFault();
    }


}